var app = getApp();
var WxParse = require('../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this;
    var id = options.id;
    var ksname = options.ksname
    var uniacid = app.siteInfo.uniacid;
    //console.log(ksname)
    var date = options.date
    var day = options.day
    var openid = wx.getStorageSync('openid');
    that.setData({
      ksname: ksname
    })
    app.util.request({
      'url': "entry/wxapp/Zhuanjiaxiangqing",
      data: {
        id: id
      },
      success: function (res) {
        that.setData({
          xiangqing: res.data.data,
          zid: id
        })
        WxParse.wxParse('article', 'html', res.data.data.z_content, that, 5);
      },
      fail: function (err) {
        console.log(err)
      },
    })

    //查询医生的所有排版时间
    app.util.request({
      'url': "entry/wxapp/Zhuanpaibid",
      data: {
        pp_id: id,
        date: date,
        day: day
      },
      success: function (res) {
        that.setData({
          zhuanpaibid: res.data.data[0],
          startTime: res.data.data.startTime
        })
        //WxParse.wxParse('article', 'html', res.data.data.z_content, that, 5);
      },
      fail: function (err) {
        console.log(err)
      },
    })
    //查询我的个人资料是否存在
    app.util.request({
      'url': "entry/wxapp/Myinforsarray",
      data: {
        openid: openid,
        uniacid: uniacid
      },
      success: function (res) {
        that.setData({
          mygerzl: res.data.data
        })
        //WxParse.wxParse('article', 'html', res.data.data.z_content, that, 5);
      },
      fail: function (err) {
        console.log(err)
      },
    })
    //查询我是否预约

  },
  // 预约支付
  yuyuezhifu: function (e) {
    var that = this
    var mygerzl = that.data.mygerzl
    var xiangqing = that.data.xiangqing
    // console.log(mygerzl, xiangqing)
    var my_id = mygerzl.my_id
    var id = xiangqing.id
    var zid = that.data.zid
    // console.log(zid)
    var uniacid = app.siteInfo.uniacid;
    var myshifouyy = that.data.myshifouyy
    var zjzztime = e.currentTarget.dataset.time
    var midday = e.currentTarget.dataset.midday
    var year = e.currentTarget.dataset.year
    var openid = wx.getStorageSync('openid');
    var ksname = that.data.ksname
    //查询我是否预约
    app.util.request({
      'url': "entry/wxapp/Myshifouyy",
      data: {
        zy_openid: openid,
        uniacid: uniacid,
        zy_riqi: midday,
        zy_type: zjzztime,
        zy_yibao: times,

      },
      success: function (res) {
        // console.log(res)
        if (res.data.data !== false) {
          wx.showToast({
            title: '您已预约',
            icon: 'success',
            duration: 800,
          })
          return false;
        } else if (mygerzl == false) {
          wx.showModal({
            content: '请完善您的个人资料后再预约',
            success: function (res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '/hyb_yl/mysubpages/pages/gerenxinxi/gerenxinxi'
                })
              }
            }
          })
          return false;
        } else {
          wx.showModal({
            content: '确认预约吗？',
            success: function (res) {
              var z_yy_money = e.currentTarget.dataset.data;
              var openid = wx.getStorageSync('openid')
              if (res.confirm) {
                app.util.request({
                  'url': 'entry/wxapp/Orderpay',
                  method: "GET",
                  data: {
                    s_openid: openid, s_ormoney: z_yy_money
                  },
                  success: function (res) {
                    wx.requestPayment({
                      'timeStamp': res.data.timeStamp,
                      'nonceStr': res.data.nonceStr,   //字符串随机数
                      'package': res.data.package,
                      'signType': res.data.signType,
                      'paySign': res.data.paySign,
                      success: function (res) {
                        console.log(res)
                        app.util.request({
                          'url': "entry/wxapp/Myzhuanjiayy",
                          data: {
                            zy_money: z_yy_money,
                            zy_name: my_id,
                            z_name: zid,
                            zy_openid: openid,
                            uniacid: uniacid,
                            zy_type: zjzztime,
                            zy_riqi: midday,
                            zy_yibao: year,
                            ksname: ksname
                          },
                          success: function (res) {
                            console.log(res)
                            wx.showToast({
                              title: '预约成功',
                              icon: 'success',
                              duration: 800,
                            })
                            app.util.request({
                              'url': 'entry/wxapp/PaysendSms',
                              'data': {
                                my_id: my_id,//用户ID
                                zid: zid//专家ID
                              },
                              success: function (res) {
                                console.log(res)
                              },
                              fail: function (err) {
                                console.log(err)
                              },
                            })

                          },
                          fail: function (err) {
                            console.log(err)
                          },
                        })
                      },
                      fail: function (error) {
                        console.log(error);
                      },
                    })
                  }
                })
              }
            }
          })
        }
      },

    })







  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})